<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.username" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.phone" placeholder="电话"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleEdit">新增</el-button>
				</el-form-item>

			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="100"></el-table-column>
			<el-table-column prop="sex" label="性别" width="120" :formatter="formatSex"></el-table-column>
			<!--<el-table-column prop="classname" label="班级" width="150"></el-table-column>-->
			<el-table-column prop="age" label="年龄" width="120"></el-table-column>
			<el-table-column prop="phone" label="电话" width="150"></el-table-column>
			<el-table-column prop="address" label="地址" width="180"></el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button size="small" @click="resetPwd(scope.$index, scope.row)">重置密码</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off" style="width:200px;" max-length="20"></el-input>
				</el-form-item>
				<el-form-item label="性别" prop="sex">
					<el-radio-group v-model="editForm.sex">
						<el-radio class="radio" :label="0" value="0">男</el-radio>
						<el-radio class="radio" :label="1" value="1">女</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="年龄">
					<el-input-number v-model="editForm.age" :min="0" :max="200"  style="width:200px;"></el-input-number>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off" style="width:200px;"></el-input>
				</el-form-item>
				<el-form-item label="生日">
					<el-date-picker type="date" placeholder="选择日期" v-model="editForm.birthday"></el-date-picker>
				</el-form-item>
				<el-form-item label="现居住地址">
					<el-input type="input" v-model="editForm.address"></el-input>
				</el-form-item>
				<!--<el-form-item label="班级" prop="classname">
					<el-select v-model="editForm.classname" filterable size="70px" placeholder="请选择" @change="handleChange($event)"  style="width:200px;">
						<el-option v-for="item in options" :key="item.id" :label="item.classname" :value="item.classname"></el-option>
					</el-select>
				</el-form-item>-->
				<el-form-item label="紧急联系人">
					<el-input type="input" v-model="editForm.emergencycontact" style="width:200px;" max-length="20"></el-input>
				</el-form-item>
				<el-form-item label="身份证号">
					<el-input type="input" v-model="editForm.idnumber" style="width:200px;" max-length="20"></el-input>
				</el-form-item>
				<el-form-item label="学历">
					<el-input type="input" v-model="editForm.education" style="width:200px;"></el-input>
				</el-form-item>
				<el-form-item label="毕业院校">
					<el-input type="input" v-model="editForm.graduated" style="width:200px;"></el-input>
				</el-form-item>
				<el-form-item label="专业">
					<el-input type="input" v-model="editForm.major" style="width:200px;"></el-input>
				</el-form-item>
				<el-form-item label="毕业时间">
					<el-date-picker type="date" placeholder="选择日期" v-model="editForm.graduationtime"></el-date-picker>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea" v-model="editForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" style="text-align:center;">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import {  removeTeacher, batchRemoveTeacher, editTeacher } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				options:[],
				users: [],
				total: 0,
				currentPage: 1,
				pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' },
					],
					phone:[
						{ required: true, message: '请填写电话', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					sex: -1
				},
				importFormVisible: false,//导入界面是否显示
				importLoading: false,
				importForm:{
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
            checkPhone(value){
            	if (!(/^1[34578]\d{9}$/.test(value))) {
                	alert("请输入正确的手机号码!");
                	return false;
                }
                return true;
            },
            appDownload(){
            	window.location.href = '../../../static/学生信息导入模板.xlsx'
            },
			getClassList(){
				this.$http.post("/class/classList").then((res) => {
					this.options = res.data;
				});
			},
			handleChange(value) {
				this.selectList = value;
				let obj = {};
				obj = this.options.find((item)=>{
					return item.classname === value;//筛选出匹配数据
				});
				this.editForm.classNum = obj.classnum;
				this.editForm.classId = obj.id;
				//this.classname = obj.classname;
			},

			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '女' : row.sex == 0 ? '男' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				let para = {
					currentPage: this.currentPage,
					pageSize: 10,
					name: this.filters.username,
					classname: this.filters.classname,
					phone: this.filters.phone,
					u_type:0
				};
				this.listLoading = true;
				this.$http.post("/teacher/listpage", para).then((res) => {
					this.total = res.data.total;
					this.users = res.data.list;
					this.listLoading = false;
				});
			},
			handleImport: function(){
				this.importFormVisible = true;
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row.id };
					removeTeacher(row.id).then((res) => {
						this.listLoading = false;
						if(res.data == ""){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						}else{
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			resetPwd:function(index,row){
				this.$confirm('确认重置密码吗？', '提示', {}).then(() => {
					this.$http.post("/user/resetpwd?id="+row.id).then((res) => {
						if(res.data == ""){
							this.$message({
								message: '重置成功',
								type: 'success'
							});
							this.getUsers();
						}else{
							this.$message({
								message: res.data,
								type: 'error'
							});
						}
					});
				});
			},

			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							let flag = this.checkPhone(para.phone);
							if(flag == false){
								this.editLoading = false;
								return;
							}
							para.u_type = 0;
							editTeacher(para).then((res) => {
								this.editLoading = false;
								if(res.data == ""){
									//NProgress.done();
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}else{
									this.$message({
										message: res.data,
										type: 'error'
									});
								}

							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					batchRemoveTeacher(ids).then((res) => {
						if(res.data == ""){
							this.listLoading = false;
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						}else{
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
			this.getClassList();
		}
	}

</script>

<style scoped>

</style>